export const getBaseUrl = (context: string | '') => {
  let location = window.document.location;
  let pagePath = location.pathname;
  context = context || '';
  if (context != '') {
    let pos = pagePath.indexOf('/' + context);
    context = pos > -1 ? context : '';
    return location.protocol + '//' + location.host + '/' + context;
  } else {
    return location.protocol + '//' + location.host;
  }
};
/**
 * 解析URL传参
 * @param {Object} key
 */
export function getQueryVariable(key: string) {
  let after = window.location.search !== '' ? window.location.search : window.location.hash;
  if (after.indexOf('?') === -1) return null; //如果url中没有传参直接返回空

  //key存在先通过search取值如果取不到就通过hash来取
  after = after.substr(after.indexOf('?') + 1) || window.location.hash.split('?')[1];

  if (after) {
    let reg = new RegExp('(^|&)' + key + '=([^&]*)(&|$)');
    let r = after.match(reg);
    if (r != null) {
      return decodeURIComponent(r[2]);
    } else {
      return null;
    }
  }
}

export const browser = {
  versions: (function () {
    let u = navigator.userAgent,
      app = navigator.appVersion;
    return {
      //移动终端浏览器版本信息
      trident: u.indexOf('Trident') > -1, //IE内核
      presto: u.indexOf('Presto') > -1, //opera内核
      webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
      gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
      mobile: !!u.match(/AppleWebKit.*Mobile.*/) || !!u.match(/AppleWebKit/), //是否为移动终端
      ios: !!u.match(/(i[^;]+;)( U;)? CPU.+Mac OS X/), //ios终端
      android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
      iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器
      iPad: u.indexOf('iPad') > -1, //是否iPad
      webApp: u.indexOf('Safari') == -1 //是否web应该程序，没有头部与底部
    };
  })(),
  language: (navigator.browserLanguage || navigator.language).toLowerCase()
};
